package com.kkd.kkdapp.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.http.entity.DealDetailItem;

public class ExchangeDetailSimpleAdapter extends BaseAdapter {

	private List<DealDetailItem> list;
	private Context mContext;
	private LayoutInflater inflater;
	private DecimalFormat df = new DecimalFormat("#,###.00");

	public ExchangeDetailSimpleAdapter(Context mContext,
			List<DealDetailItem> list) {
		this.list = list;
		this.mContext = mContext;
		inflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolderContent viewHolderContent;
		if (convertView == null) {
			viewHolderContent = new ViewHolderContent();
			convertView = inflater.inflate(
					R.layout.item_content_pinner_listview, parent, false);
			convertView = inflater.inflate(
					R.layout.item_content_pinner_listview, parent, false);
			viewHolderContent.tv_title = (TextView) convertView
					.findViewById(R.id.tv_title);
			viewHolderContent.tv_time = (TextView) convertView
					.findViewById(R.id.tv_time);
			viewHolderContent.tv_num = (TextView) convertView
					.findViewById(R.id.tv_num);
			viewHolderContent.tv_flag = (TextView) convertView
					.findViewById(R.id.tv_flag);
			viewHolderContent.line = convertView.findViewById(R.id.line);
			viewHolderContent.creditor_number_tv = (TextView) convertView
					.findViewById(R.id.creditor_number_tv);
			convertView.setTag(viewHolderContent);
		} else {
			viewHolderContent = (ViewHolderContent) convertView.getTag();
		}

		viewHolderContent.tv_title.setSelected(true);
		if ("3018".equals(list.get(position).getTradeType())
				&& !TextUtils.isEmpty(list.get(position).getExt())) {// 说明是邀请好友返利
			viewHolderContent.tv_title.setText(list.get(position).getExt());
		} else {
			viewHolderContent.tv_title.setText(list.get(position).getSummary());
		}

		String timeAndExtra = list.get(position).getCreateTime()
				.replaceAll("-", "/");
		viewHolderContent.tv_time.setText(timeAndExtra);
		String amountString = df.format(list.get(position).getAmount());
		String value = amountString.startsWith(".") ? ("0" + amountString)
				: amountString;
		if (list.get(position).getType() == 0) {
			viewHolderContent.tv_num.setText("-" + value);
			viewHolderContent.tv_num.setTextColor(Color.parseColor("#75d441"));
		} else {
			viewHolderContent.tv_num.setText("+" + value);
			viewHolderContent.tv_num.setTextColor(Color.parseColor("#ff7301"));
		}
		int status = list.get(position).getStatus();
		String statusName = "";
		if ("3004".equals(list.get(position).getTradeType())
				|| "3005".equals(list.get(position).getTradeType())) {
			if (status == 0) {
				statusName = "未入账";
			} else if (status == 1) {
				statusName = "已入账";
			} else if (status == 2) {
				statusName = "入账失败";
			}
			// statusName = (status == 1 ? "已入账" : "未入账");
		} else {
			statusName = "已入账";
		}

		if ("未入账".equals(statusName)) {
			viewHolderContent.tv_flag.setText("未入账");
			viewHolderContent.tv_flag.setVisibility(View.VISIBLE);
		} else if ("入账失败".equals(statusName)) {
			viewHolderContent.tv_flag.setText("入账失败");
			viewHolderContent.tv_flag.setVisibility(View.VISIBLE);
		} else {
			viewHolderContent.tv_flag.setVisibility(View.GONE);
		}
		// 债权份数处理
		String parts = list.get(position).getParts();
		if (TextUtils.isEmpty(parts)) {
			viewHolderContent.creditor_number_tv.setVisibility(View.GONE);
		} else {
			viewHolderContent.tv_flag.setVisibility(View.GONE);
			viewHolderContent.creditor_number_tv.setVisibility(View.VISIBLE);
			viewHolderContent.creditor_number_tv.setText(parts);
		}

		if (position == list.size() - 1) {
			viewHolderContent.line.setVisibility(View.INVISIBLE);
		} else {
			viewHolderContent.line.setVisibility(View.VISIBLE);
		}

		return convertView;
	}

	class ViewHolderContent {
		TextView tv_title;
		TextView tv_time;
		TextView tv_num;
		TextView tv_flag;
		TextView creditor_number_tv;
		View line;
	}

}
